/*
 * @(#)WorkflowPriorityTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.taglib;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.workflow.bean.WorkflowProgress;

/**
 * @author           Tony Chen
 * @company          DCIVision Limited
 * @creation date    21/10/2005
 * @version          $Revision: 1.4 $
 */
public class WorkflowPriorityTag extends BodyTagSupport {
  public static final String REVISION = "$Revision: 1.4 $";

  protected Log log = LogFactory.getLog(this.getClass().getName());

  private WorkflowProgress workflowProgress = null;
  
  
  
	public WorkflowProgress getWorkflowProgress() {
		return workflowProgress;
	}
  
	public void setWorkflowProgress(WorkflowProgress workflowProgress) {
		this.workflowProgress = workflowProgress;
	}
  
  public WorkflowPriorityTag(){
    super();
  }
    
  public int doStartTag() throws JspException {
    try {
      init();
      pageContext.getOut().print(this.getContent()) ;
    } catch (ApplicationException ae){
      log.error(ae.getMsgCode(), ae);
    } catch (Exception e) {
      log.fatal(e.getMessage(), e);
    } finally {
      this.release();
    }
    return(SKIP_BODY);
  }

  public int doEndTag() throws JspException {
    return EVAL_PAGE;
 }

  public void release() {
    super.release();
    this.setWorkflowProgress(null);
  }

  protected boolean validateTag(){
    return ( workflowProgress != null);
  }

  protected String getContent() throws Exception {
    //JspWriter out = pageContext.getOut();//Delete by TC for Ajax-Used    
    StringBuffer content = new StringBuffer();
    String dueDateLable = "";
    String color = "";
    
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    
    content.append("<b><font color=\'red\'>");
    
    String priority = "";
    String excalmatoryMark = "";
    if(!Utility.isEmpty( (workflowProgress).getProgressPriority() )){
      priority = (workflowProgress).getProgressPriority();
    } else {
      priority = (workflowProgress).getPriority();
    }
    
    for (int i = 0; i < Integer.parseInt(priority); i++) {
      excalmatoryMark += "!";
    }
    
    content.append(excalmatoryMark);
    content.append("</font><b>");   
    
    if(Utility.isEmpty(excalmatoryMark)){
      //content = new StringBuffer();
    }
    //out.println(content.toString());
    return content.toString();
  }

  protected void init() throws ApplicationException {    
    if ( !validateTag() ){
      throw new IllegalArgumentException();
    }
  }
 
}



